<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('users_model');
		$this->load->library('session');
		$this->load->library('form_validation');
		$this->load->library('security');
		$this->load->library('tank_auth');
		$this->load->helper('url');
		$this->load->helper('form');
		$this->lang->load('tank_auth');
		
		// prevent caching
		$this->output->set_header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
		$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
		$this->output->set_header('Cache-Control: post-check=0, pre-check=0',false);
		$this->output->set_header('Pragma: no-cache');
		
		// if session, redirect to home page		
		if($this->tank_auth->is_logged_in())
		{
			redirect('admin/home', 'refresh');
		}
	}
	
	function index()
	{
		$data['login_by_username'] = ($this->config->item('login_by_username', 'tank_auth') && $this->config->item('use_username', 'tank_auth'));
		$data['login_by_email'] = $this->config->item('login_by_email', 'tank_auth');

		$this->form_validation->set_rules('username', 'Usuario', 'trim|required|xss_clean');
		$this->form_validation->set_rules('password', 'Contraseña', 'trim|required|xss_clean');

		// Get login for counting attempts to login
		if ($this->config->item('login_count_attempts', 'tank_auth') && ($login = $this->input->post('username')))
		{
			$login = $this->security->xss_clean($login);
		}
		else
		{
			$login = '';
		}
		
		if ($this->tank_auth->is_max_login_attempts_exceeded($login))
		{
			$this->form_validation->set_rules('captcha', 'Código', 'trim|xss_clean|required|callback__check_captcha');
		}
		$data['errors'] = array();
		
		if ($this->form_validation->run())
		{
			$successful_login = $this->tank_auth->login(
				$this->form_validation->set_value('username'),
				$this->form_validation->set_value('password'),
				FALSE,
				$data['login_by_username'],
				$data['login_by_email']
			);
			if ($successful_login)
			{
				redirect('admin/home', 'refresh');
			}
			else
			{
				$errors = $this->tank_auth->get_error_message();
				foreach ($errors as $k => $v)	$data['errors'][$k] = $this->lang->line($v);
			}
		}
		$data['show_captcha'] = FALSE;
		if ($this->tank_auth->is_max_login_attempts_exceeded($login))
		{
			$data['show_captcha'] = TRUE;
			$data['captcha_html'] = $this->_create_captcha();
		}
		$this->load->view('admin/login', $data);
	}

	/**
	 * Create CAPTCHA image to verify user as a human
	 *
	 * @return	string
	 */
	function _create_captcha()
	{
		$this->load->helper('captcha');

		$cap = create_captcha(array(
			'img_path'		=> './'.$this->config->item('captcha_path', 'tank_auth'),
			'img_url'		=> base_url().$this->config->item('captcha_path', 'tank_auth'),
			'font_path'		=> './'.$this->config->item('captcha_fonts_path', 'tank_auth'),
			'font_size'		=> $this->config->item('captcha_font_size', 'tank_auth'),
			'img_width'		=> $this->config->item('captcha_width', 'tank_auth'),
			'img_height'	=> $this->config->item('captcha_height', 'tank_auth'),
			'show_grid'		=> $this->config->item('captcha_grid', 'tank_auth'),
			'expiration'	=> $this->config->item('captcha_expire', 'tank_auth'),
		));

		// Save captcha params in session
		$this->session->set_flashdata(array(
				'captcha_word' => $cap['word'],
				'captcha_time' => $cap['time'],
		));

		return $cap['image'];
	}

	/**
	 * Callback function. Check if CAPTCHA test is passed.
	 *
	 * @param	string
	 * @return	bool
	 */
	function _check_captcha($code)
	{
		$time = $this->session->flashdata('captcha_time');
		$word = $this->session->flashdata('captcha_word');

		list($usec, $sec) = explode(" ", microtime());
		$now = ((float)$usec + (float)$sec);

		if ($now - $time > $this->config->item('captcha_expire', 'tank_auth')) {
			$this->form_validation->set_message('_check_captcha', $this->lang->line('auth_captcha_expired'));
			return FALSE;

		} elseif (($this->config->item('captcha_case_sensitive', 'tank_auth') AND
				$code != $word) OR
				strtolower($code) != strtolower($word)) {
			$this->form_validation->set_message('_check_captcha', $this->lang->line('auth_incorrect_captcha'));
			return FALSE;
		}
		return TRUE;
	}
}

/* End of file login.php */
/* Location: ./application/controllers/admin/login.php */